﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iOak.Core.DataExchange;

using LateBindingApi.Core;
using Excel = NetOffice.ExcelApi;
using NetOffice.ExcelApi.Enums;
using System.Reflection;
using iOak.Core;
using System.Web;
using System.Drawing;
using iOak.Domain.ReportDomain;
using iOak.Business.Campaigns;
namespace iOak.Web.ExeclHelper
{
    public class ExportManager
    {
        public static readonly Dictionary<int, string> ColList = new Dictionary<int, string>();
        private static readonly CampaignService _campaignService = new CampaignService();

        static ExportManager()
        {
            ColList.Add(0, "A");
            ColList.Add(1, "B");
            ColList.Add(2, "C");
            ColList.Add(3, "D");
            ColList.Add(4, "E");
            ColList.Add(5, "F");
            ColList.Add(6, "G");
            ColList.Add(7, "H");
            ColList.Add(8, "I");
            ColList.Add(9, "J");
            ColList.Add(10, "K");
            ColList.Add(11, "L");
            ColList.Add(12, "M");
            ColList.Add(13, "N");
            ColList.Add(14, "O");
            ColList.Add(15, "P");
            ColList.Add(16, "Q");
            ColList.Add(17, "R");
            ColList.Add(18, "S");
            ColList.Add(19, "T");
            ColList.Add(20, "U");
            ColList.Add(21, "V");
            ColList.Add(22, "W");
        }

        public static void ExportToXls(string filePath, List<string> nameList)
        {
            using (ExcelHelper excelHelper = new ExcelHelper(filePath))
            {
                excelHelper.Hdr = "YES";
                excelHelper.Imex = "0";
                Dictionary<string, string> tableDefinition = new Dictionary<string, string>();
                nameList.ForEach(item =>
                    {
                        tableDefinition.Add(item, "nvarchar(100)");
                    });

                excelHelper.WriteTable("导出模板", tableDefinition);
            }
        }

        public static void ExportToExecl(List<string> nameList,out string filePath, out string fileName)
        {
            // 创建execl application
            LateBindingApi.Core.Factory.Initialize();
            Excel.Application excelApplication = new Excel.Application();
            excelApplication.DisplayAlerts = false;

            // 创建工作簿
            Excel.Workbook workBook = excelApplication.Workbooks.Add();

            // 创建sheet
            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

            // 保存数据
            int count = nameList.Count;
            for (int i = 0; i < count; i++)
            {
                workSheet.get_Range(ColList[i] + "1").Value = nameList[i];
                workSheet.Columns[i + 1, Missing.Value].NumberFormat = "@";
            }

            // 设定名称
            fileName = string.Format("简历模板{0}", GetDefaultExtension(excelApplication));

            // 设定路径
            filePath = string.Format("{0}files\\ExportImport\\{1}", HttpContext.Current.Request.PhysicalApplicationPath, fileName);

            // 保存
            workBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive);

            excelApplication.Quit();
            excelApplication.Dispose();
        }



        public static string GetDefaultExtension(Excel.Application application)
        {
            double version = Convert.ToDouble(application.Version);
            if (version >= 120.00)
                return ".xlsx";
            else
                return ".xls";
        }

        public static void SetCell(Excel.Worksheet workSheet, string cell, bool isBold, string value, int size, bool isBorders)
        {
            workSheet.get_Range(cell).Value = value;
            workSheet.get_Range(cell).Font.Bold = isBold;
            workSheet.get_Range(cell).Font.Name = "宋体";
            workSheet.get_Range(cell).Font.Size = size;
            workSheet.get_Range(cell).VerticalAlignment = XlHAlign.xlHAlignCenter;
            workSheet.get_Range(cell).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            if (isBorders)
            {
                workSheet.get_Range(cell).Borders.Color = Color.Black;
            }
        }

        public static void CreateSumExecl(Guid campaignId)
        {
            var item = _campaignService.Retrieve(campaignId);
            var listFiled = ExeclReportHelper.GetFiledList(item);
            var classTye = typeof(ExeclDetial);
            var listValue = ExeclReportHelper.GetExeclData(item, listFiled);
            dynamic orderList = ExeclReportHelper.OrderByDataList(listValue, listFiled);
            var sumnumber = ExeclReportHelper.GetSumColList(listValue, listFiled);
            CollegeExeclHelper.CreateExecl(listFiled, orderList, classTye, sumnumber);
        }
    }
}
